function A(cb) {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log('异步A');
      cb()
      resolve()
    }, 1000)
  })
}
function B(cb) {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log('异步B');
      cb()
      resolve()
    }, 500)
  })
}
function C(cb) {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log('异步C');
      cb()
      resolve()
    }, 100)
  })
}


function simpleThunk(fn) {
  return function(callback) {
    fn(function(result) {
      callback(result)
    })
  }
}



function * exampleGenerator() {
  yield simpleThunk(A)  
  yield simpleThunk(B)
  yield simpleThunk(C)
}

function run(generatorFunction) {
  const g = generatorFunction()
  // g.next().value()

  function iterate(g) {
    const {value, done} = g.next()  // {value: function(callback){}, done: false}
    if (done) return

    if (typeof value === 'function') {
      value(() => {
        iterate(g)
      })
    } else {
      throw new Error('Generator function should return a Thunk function')
    }
    
  }
  iterate(g)

}

run(exampleGenerator)

// const foo1 = simpleThunk(A)
// const foo2 = simpleThunk(B)
// const foo3 = simpleThunk(C)
// foo1(() => {})


